<template>
	<view>
		<!-- 搜索组件 -->
		<my-search bgc="#c00000" :radius="40" @click="toSearch"></my-search>
		<view class="box">
			<!-- 左边导航 -->
			<scroll-view class="left" :style="{height:`${wh}px`}" scroll-y="true">
				<view @click="changeAcitve(index)" class="item" :class="{active :active=== index}"
					v-for="(item,index) in cateList" :key="index">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 右边列表 -->
			<scroll-view :scroll-top='scrollTop' class="right" scroll-y="true" :style="{height:`${wh}px`}">
				<view class="rightbox" v-for="(item,index) in subCateList" :key="index">
					<!-- 渲染二级数据 -->
					<view class="title">
						{{item.cat_name}}
					</view>
					<!-- 渲染三级数据 -->
					<view class="itembox">
						<view class="item" @click="toList(subitem.cat_name)"
							v-for="(subitem,subindex) in item.children">
							<image :src="subitem.cat_icon" mode="widthFix"></image>
							<text>{{subitem.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import mymixins from '../../mixins/index.js'
	export default {
		mixins: [mymixins],
		data() {
			return {
				wh: 0,
				active: 0,
				cateList: [],
				subCateList: [],
				scrollTop: 0
			};
		},
		methods: {
			// 点击左侧列表
			changeAcitve(index) {
				this.active = index
				this.subCateList = this.cateList[this.active].children
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			// 获取右侧列表信息
			async getCateList() {
				const res = await uni.$http.get('/api/public/v1/categories')
				console.log(res);
				this.cateList = res.data.message
				this.subCateList = this.cateList[this.active].children
			},
			// 跳转至list页面
			toList(name) {
				uni.navigateTo({
					url: `/pages/list/list?key=${name}`
				})
			},
			// 跳转至搜索页
			toSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			}
		},
		onLoad() {
			uni.getSystemInfo({
				success: res => {
					this.wh = res.windowHeight - 55
				}
			})
			this.getCateList()
		}
	}
</script>

<style lang="scss">
	.box {
		display: flex;
		font-size: 12px;

		.left {
			width: 200rpx;

			.item {
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
				background-color: #f7f7f7;

				&.active {
					background-color: #fff;
					position: relative;

					&::before {
						content: '';
						height: 30px;
						width: 2px;
						background-color: #c00000;
						position: absolute;
						top: 50%;
						left: 0;
						transform: translateY(-50%);
					}
				}
			}

		}

		.right {
			.title {
				font-weight: bold;
				text-align: center;
				line-height: 100rpx;
			}

			.itembox {
				display: flex;
				flex-wrap: wrap;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					width: 33.33%;
					margin-bottom: 10px;

					image {
						width: 60%;
					}
				}
			}
		}
	}
</style>
